package gbench.appdemo.mall.erp;

import gbench.appdemo.mall.erp.components.IEntity;
import gbench.common.tree.LittleTree.IRecord;

/**
 * 简单实体
 * 
 * @author gbench
 *
 */
public class Entity<S> implements IEntity<S> {

    /**
     * 
     * @return 名称
     */
    public String getName() {
        return this.name;
    }

    /**
     * 字符串
     */
    public String toString() {
        return this.name;
    }

    /**
     * 
     * @return id
     */
    public Integer getId() {
        return this.id;
    }

    /**
     * 提取属性值
     * 
     * @param <T> 属性类型
     * @param name         属性名
     * @param defaultValue 默认值
     * @return 属性值
     */
    public <T> T attr(final String name, T defaultValue) {
        return attributes.get(name, defaultValue);
    }

    /**
     * @return the attributes
     */
    public IRecord getAttributes() {
        return attributes;
    }

    /**
     * @param attributes the attributes to set
     */
    @SuppressWarnings("unchecked")
    public S setAttributes(final IRecord attributes) {
        this.attributes = attributes;
        return (S) this;
    }

    /**
     * 探求哈希吗
     */
    public int hashCode() {
        return this.id.hashCode();
    }

    /**
     * 判断对象是否相等
     */
    public boolean equals(final Object obj) {
        if (obj == null)
            return false;
        final var cls = this.getClass();
        if (cls != obj.getClass()) {
            return false;
        } else {
            return this.getId().equals(((Entity<?>) obj).getId());
        } // if
    }

    protected Integer id;
    protected String name;
    protected String description;
    protected IRecord attributes;
}